/*路由守卫*/
import router from '@/router'
import {setRouterFromNet} from "@/assets/utils/getAsyncRouters";
//路由列表
// //element 提示框
// import {Message} from 'element-ui';
//
import Vue from "vue";
// //
let Vm = new Vue()

import store from '@/store'

function creatNewRouters() {
    let netRouter = JSON.parse(window.sessionStorage.getItem('RoutFromNet'))
    if (netRouter) {
        setRouterFromNet(netRouter)
    }
}

let flag = true

//在跳转之前
router.beforeEach((to, from, next) => {
        // console.log('666',to.name)
        //title ico图标
        window.document.title = "项目管理平台";
        // let uAddRouter = JSON.parse(sessionStorage.getItem('storeData')).dynamicRoutes

        //处理页面刷新路由丢失
        if (flag) {
            flag = false
            console.log('123')
            creatNewRouters()
            // next({...to,replace:true})
            next(to)
        } else {
            next()
        }


        // next()
        // let newLink = document.getElementsByTagName('link');
        // newLink[0].outerHTML = "<link rel='shortcut icon' href='https://gd-hbimg.huaban.com/91835998656e77b9d6486f86ec2e98c1ff97b59212df0-xQRcxt_fw658'>"
        //头部tab切换
        if (to.name) {
            if (window.sessionStorage.getItem('tabsViewArray')) {
                let tabsViewArray = JSON.parse(window.sessionStorage.getItem('tabsViewArray'));
                let whetherTabs = false;
                for (let i = 0; i < tabsViewArray.length; i++) {
                    if (tabsViewArray[i].fullPath === to.fullPath) {
                        whetherTabs = true
                    }
                }
                if (!whetherTabs) {
                    tabsViewArray.push({
                        name: to.name,
                        path: to.path,
                        query: {},
                        fullPath: to.fullPath,
                        title: to.name,
                        iconCloseShow: true
                    })

                    Vm.resetSetItem('tabsViewArray', JSON.stringify(tabsViewArray));
                }
            }
        }
    // console.log('777',to.meta.title)
        //缓存
        if (!store.state.cachedViews.includes(to.meta.title)) {
            store.state.cachedViews.push(to.meta.title);
        }
        // }
        //
        // // 登录页面放行 不在左侧的放行
        // if (to.path == '/login' || to.meta.authority.indexOf(0) != -1) {
        //     next();
        //     //权限为100的放行  左侧都可以访问
        // } else if (to.meta.authority.indexOf(100) != -1) {
        //     next();
        //     //符合权限的放行
        // } else if (to.meta.authority.indexOf(Number(window.localStorage.getItem('currentPermissions'))) != -1) {
        //     routerList.saveList();
        //     next();
        //     //不合符权限的限制，并跳转回login页
        // } else {
        //     Message({
        //         message: '无权限访问',
        //         type: 'error',
        //         duration: 3 * 1000
        //     })
        //     next('/login');
        // }
    }
)

//在跳转之后
router.afterEach(() => {

})
